<template>
  <!-- <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  > -->
  <div class="reqVisit">
    <h1>打卡单</h1>
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="打卡地点" prop="signInSite">
        <el-input v-model="dataForm.signInSite" placeholder=""></el-input>
      </el-form-item>

      <el-form-item label="健康码" prop="healthCode">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img
            v-if="dataForm.healthCode"
            :src="dataForm.healthCode"
            class="avatar"
            alt="头像"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="是否隔离" prop="isScoped">
        <el-tag v-if="dataForm.isScoped == 1" size="small">是</el-tag>
        <el-tag v-if="dataForm.isScoped == 0" size="small">否</el-tag>
        <!-- <el-input v-model="dataForm.isOut" placeholder=""></el-input> -->
        <!-- <el-radio v-model="dataForm.isScoped" disabled :label="1">是</el-radio>
        <el-radio v-model="dataForm.isScoped" disabled :label="0">否</el-radio> -->
      </el-form-item>

      <el-form-item label="核酸报告" v-if='dataForm.isScoped == 1' prop="testResult">
        <el-upload
          class="avatar-uploader"
          :action="uploadUrl"
          :show-file-list="false"
          :on-success="handleAvatarSuccessTest"
          :before-upload="beforeAvatarUpload"
        >
          <img
            v-if="dataForm.testResult"
            :src="dataForm.testResult"
            class="avatar"
            alt="头像"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="是否外出" prop="isOut">
        <!-- <el-input v-model="dataForm.isOut" placeholder=""></el-input> -->
        <el-radio v-model="dataForm.isOut" :label="1">是</el-radio>
        <el-radio v-model="dataForm.isOut" :label="0">否</el-radio>
      </el-form-item>
      <el-form-item label="外出地点" v-if="dataForm.isOut == 1" prop="outSite">
        <el-input v-model="dataForm.outSite" placeholder=""></el-input>
      </el-form-item>
      <el-form-item
        label="外出轨迹"
        v-if="dataForm.isOut == 1"
        prop="out_track"
      >
        <el-input
          type="textarea"
          v-model="dataForm.out_track"
          placeholder=""
        ></el-input>
      </el-form-item>
      <el-form-item label="当日体温" prop="temperature">
        <el-input-number
          :min="35"
          size="mini"
          v-model="dataForm.temperature"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </div>
  <!-- </el-dialog> -->
</template>

<script>
import { getUserInfo } from '@api/admin'
export default {
  data () {
    return {
      visible: 1,
      uploadUrl: window.SITE_CONFIG.baseUrl + '/covid/file/upload',

      dataForm: {
        id: 0,
        signInTime: '',
        signInSite: '',
        status: '',
        isOut: '',
        outSite: '',
        out_track: '',
        temperature: '',
        userId: '',
        updateTime: '',
        createTime: '',
        testResult: '',
        isScoped: '',
        healthCode: ''
      },
      dataRule: {
        signInTime: [{ required: true, message: '不能为空', trigger: 'blur' }],
        signInSite: [{ required: true, message: '不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '不能为空', trigger: 'blur' }],
        isOut: [{ required: true, message: '不能为空', trigger: 'blur' }],
        outSite: [{ required: true, message: '不能为空', trigger: 'blur' }],
        out_track: [{ required: true, message: '不能为空', trigger: 'blur' }],
        temperature: [{ required: true, message: '不能为空', trigger: 'blur' }],
        userId: [{ required: true, message: '不能为空', trigger: 'blur' }],
        updateTime: [{ required: true, message: '不能为空', trigger: 'blur' }],
        createTime: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    userId: {
      get () {
        return this.$store.state.user.id
      }
    }
  },
  created () {
    getUserInfo().then(({ data }) => {
      const { infectionStatus } = data.user
      if (infectionStatus == 0) {
        this.dataForm.isScoped = 0
      } else {
        this.dataForm.isScoped = 1
      }
    })
  },
  methods: {
    // 核酸
    handleAvatarSuccessTest (res, file) {
      this.dataForm.testResult = res.msg
    },
    handleAvatarSuccess (res, file) {
      this.dataForm.healthCode = res.msg
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },

    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/covid/signinrecord/info/${this.dataForm.id}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.signInTime = data.signInRecord.signInTime
              this.dataForm.signInSite = data.signInRecord.signInSite
              this.dataForm.status = data.signInRecord.status
              this.dataForm.isOut = data.signInRecord.isOut
              this.dataForm.outSite = data.signInRecord.outSite
              this.dataForm.out_track = data.signInRecord.out_track
              this.dataForm.temperature = data.signInRecord.temperature
              this.dataForm.userId = data.signInRecord.userId
              this.dataForm.updateTime = data.signInRecord.updateTime
              this.dataForm.createTime = data.signInRecord.createTime
              this.dataForm.healthCode = data.signInRecord.healthCode
              this.dataForm.testResult = data.signInRecord.testResult
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/covid/signinrecord/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              // 'signInTime': this.dataForm.signInTime,
              signInSite: this.dataForm.signInSite,
              // 'status': this.dataForm.status,
              isOut: this.dataForm.isOut,
              outSite: this.dataForm.outSite,
              out_track: this.dataForm.out_track,
              healthCode: this.dataForm.healthCode,
              testResult: this.dataForm.testResult,
              temperature: this.dataForm.temperature.toString(),
              userId: this.userId
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
.reqVisit {
  display: flex;
  flex-direction: column;
  margin: 0 auto;
  width: 70%;
  justify-content: center;
  h1,
  .mid {
    margin: 20px auto;
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
